package com.match.util;

import com.match.entry.Mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import java.util.Properties;

public class MailSessionUtil {
    /**
     * 生成mail的session
     * @param mail
     * @return
     */
    public static Session createSessionUtil(Mail mail){
        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                // 密码验证
                return new PasswordAuthentication(mail.getUserName(), mail.getPassword());
            }
        };
        /*
         *Properties是一个属性对象，用来创建Session对象
         */
        Properties properties = new Properties();
//        properties.put("mail.pop3.ssl.enable", mail.isSSL());
//        properties.put("mail.pop3.host", mail.getHost());
//        properties.put("mail.pop3.port", mail.getPort());
        properties.setProperty("mail.host", mail.getHost());
        properties.setProperty("mail.transport.protocol", mail.getProtocol());
        properties.setProperty("mail.smtp.auth", mail.isAuth() + "");
        properties.setProperty("mail.smtp.socketFactory.class", mail.getSocketFactory());
        properties.setProperty("mail.smtp.port", mail.getPort() + "");
        properties.setProperty("mail.smtp.socketFactory.port", mail.getPort() + "");
        /*
         *Session类定义了一个基本的邮件对话。
         */
        return Session.getInstance(properties, auth);
    }
}
